package zhibi.cms.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import zhibi.cms.domain.Article;
import zhibi.cms.domain.Site;
import zhibi.cms.extra.base.AdminBaseController;
import zhibi.cms.extra.context.Constant;
import zhibi.cms.extra.pojo.ArticlePo;
import zhibi.cms.service.ArticleService;
import zhibi.cms.service.SiteService;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping(Constant.SYS_PREFIX + "/admin/article")
public class CmsAdminArticleController extends AdminBaseController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private SiteService siteService;

    /**
     * 列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, ArticlePo articlePo) {
        Example example = Example.getInstance()
                .addParam("a.title", articlePo.getTitle(), ExampleType.Operation.LIKE)
                .addParam("s.name", articlePo.getSiteName(), ExampleType.Operation.LIKE)
                .addOrder("a.sort", ExampleType.OrderType.DESC)
                .addOrder("a.addtime", ExampleType.OrderType.DESC);
        PageInfo<ArticlePo> pageInfo = articleService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return Constant.SYS_PREFIX + "/article/list";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        List<Site> siteList = siteService.selectAll();
        model.addAttribute("siteList", siteList);
        return Constant.SYS_PREFIX + "/article/add";
    }

    /**
     * 添加文章
     *
     * @return
     */
    @PostMapping("add")
    public String add(Article article, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            article.setShowpic(saveFile(file, "IMG"));
        }
        article.setStatus(1);
        article.setAddtime(new Date());
        articleService.insertSelective(article);
        return redirect("list");
    }

    /**
     * 详情
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable String id) {
        Article article = articleService.selectByPrimaryKey(id);
        if (null == article) throw new MessageException("该文章不存在");
        List<Site> siteList = siteService.selectAll();
        model.addAttribute("siteList", siteList);
        model.addAttribute("article", article);
        return Constant.SYS_PREFIX + "/article/edit";
    }

    /**
     * 更新
     *
     * @param article
     * @return
     */
    @PostMapping("update")
    public String update(Article article, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            article.setShowpic(saveFile(file, "IMG"));
        }
        articleService.updateByPrimaryKeySelective(article);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable String id) {
        Article article = articleService.selectByPrimaryKey(id);
        if (null == article) throw new MessageException("该文章不存在");
        articleService.delete(article);
        return refresh();
    }

    /**
     * 更新状态
     *
     * @param id
     * @return
     */
    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable String id) {
        Article article = articleService.selectByPrimaryKey(id);
        if (null == article) throw new MessageException("该文章不存在");
        article.setStatus((article.getStatus() + 1) % 2);
        articleService.updateByPrimaryKeySelective(article);
        return refresh();
    }
}
